package com.csq.portal.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csq.cms.service.FavoriteService;
import com.csq.cms.service.RedisService;
import com.csq.common.pojo.ReturnResult;
import com.csq.manager.pojo.Account;
import com.csq.manager.pojo.Favorite;

@Controller
@RequestMapping("/favorite")
public class FavoriteController {

	@Autowired
	private FavoriteService favoriteService;

	@Autowired
	private RedisService redisService;

	@RequestMapping("/parttime")
	@ResponseBody
	public ReturnResult addFavoriteParttime(Integer parttimeId,
			HttpServletRequest request) {
		//取出用户名
		Account account = (Account) request.getSession()
				.getAttribute("account");

		//更新redis
		Account redisAccount = redisService.getRedisAccount(account.getEmail());

		request.getSession().setAttribute("account", redisAccount);
		Favorite favorite = new Favorite();
		favorite.setParttimeId(parttimeId);
		favorite.setUserId(redisAccount.getUserId());

		ReturnResult addFavorite = favoriteService.addFavorite(favorite);

		return addFavorite;
	}

	@RequestMapping("/company")
	@ResponseBody
	public ReturnResult addFavoriteEnterprise(Integer enterpriseId,
			HttpServletRequest request) {
		//取出用户名
		Account account = (Account) request.getSession()
				.getAttribute("account");

		//更新redis
		Account redisAccount = redisService.getRedisAccount(account.getEmail());

		request.getSession().setAttribute("account", redisAccount);
		Favorite favorite = new Favorite();
		favorite.setEnterpriseId(enterpriseId);
		favorite.setUserId(redisAccount.getUserId());

		ReturnResult addFavorite = favoriteService.addFavorite(favorite);

		return addFavorite;
	}

}
